#include "stackADT.h"
#include <stdlib.h>
#include <stdio.h>
struct node
{
    Item data;
    struct node *next;
};

struct stack_type
{
    struct node *top;
};

static void terminate(const char *mess)
{
    printf("%s\n", mess);
    exit(EXIT_FAILURE);
}

Stack create(void)
{
    Stack s = malloc(sizeof(*s));
    if (!s)
        terminate("Error in create: stack could not be created.");
    s->top = NULL;
    return s;
}

void destroy(const Stack s)
{
    make_empty(s);
    free(s);
}

bool is_empty(const Stack s)
{
    return s->top == NULL;
}

bool is_full(const Stack s)
{
    return false;
}

void make_empty(const Stack s)
{
    while (!is_empty(s))
        pop(s);
}

void push(Stack s, Item i)
{
    struct node *new_node = malloc(sizeof(*new_node));
    if (!new_node)
        terminate("Error in push: stack is full");
    new_node->data = i;
    new_node->next = s->top;
    s->top = new_node;
}

Item pop(Stack s)
{
    struct node *old_top = s->top;
    Item tmp = old_top->data;
    if (is_empty(s))
        terminate("Error in pop: stack is empty.");
    s->top = old_top->next;
    free(old_top);
    return tmp;
}